//
//  AtBlock.swift
//  Tinsecret_TW
//
//  Created by 崔世强 on 2018/10/30.
//  Copyright © 2018 tst. All rights reserved.
//

import Foundation


/// 表示一个@块
class AtBlock {
    var text:String!
    var segments = [AtSegment]()
    
    init(text:String) {
        self.text = "@"+text+" "
    }
    
    func addSegment(start:Int) -> AtSegment {
        let end = self.text.count - 1 + start
        let atSegment = AtSegment(start:start,end:end)
        self.segments.append(atSegment)
        return atSegment
    }
    
    
    /// @块右移
    ///
    /// - Parameters:
    ///   - start: 插入前的光标位置
    ///   - length: 右移长度
    func moveRight(start:Int,length:Int) {
        print("\(text) moveRight--before:\(segments)")
        segments = segments.map {segment in
            if start > segment.start && start <= segment.end {
                segment.end += length
                segment.broken = true
            } else if start <= segment.start {
                segment.start += length
                segment.end += length
            }
            return segment
        }
        print("\(text) moveRight--after:\(segments)")
    }
    
    
    /// @块左移
    ///
    /// - Parameters:
    ///   - start: 删除前光标位置
    ///   - length: 删除的文字的长度
    func moveLeft(start:Int,length:Int) {
        let after = start - length
        print("\(text) moveleft--before:\(segments)")
        segments = segments.filter{ (segment) -> Bool in
            
            if start >= segment.start {
                if after <= segment.start {
                    return false
                } else if after < segment.end {
                    segment.broken = true
                    segment.end -= length
                    return true
                } else {
                    return true
                }
            } else {
                segment.start -= length
                segment.end -= length
                
                return true
            }
        }
        print("\(text) moveleft--after:\(segments)")
    }
    
    
    /// 获取当前@的用户的第一个有效的@块位置，-1表示没有有效位置
    ///
    /// - Returns: 有效位置
    func getFirstSegmentStart() -> Int {
        var start = -1
        segments.forEach {
            if $0.broken {
                return
            }
            if start == -1 || $0.start < start {
                start = $0.start
            }
        }
        return start
    }
    
    
    /// 获取当前@的用户的最后一个位置块
    ///
    /// - Parameter end: 光标位置
    /// - Returns: @位置块
    func findLastSegmentByEnd(end:Int) -> AtSegment? {
        let position = end
        for segment in segments {
            if !segment.broken && segment.end == position {
                return segment
            }
        }
        return nil
    }
    
    func valid() -> Bool {
        if segments.count == 0 {
            return false
        }
        for segment in segments {
            if !segment.broken {
                return true
            }
        }
        return false
    }
    
}
class AtSegment:CustomStringConvertible {
    
    var start:Int
    var end:Int
    
    
    var description: String {
        return "(\(start), \(end))"
    }
    
    /// 表示当前位置块是否有效
    var broken = false
    
    init(start:Int,end:Int) {
        self.start = start
        self.end = end
    }
}
